<?php use_helper('jQuery')?>
<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>
<?php if($form->getObject()->isNew() || $form->getObject()->getSectionId()): ?>
    <?php echo jq_javascript_tag(
        'initElementBannerFileUpload("'.
            url_for('element_tempfileupload',$form->getObject()->getType()).
            '?section_id='.$form->getObject()->getSectionId().'&portal_id='.
            $form->getObject()->getSection()->getPortalId().
        '");'.
        'bannerBindChangeEvent();'
    )?>
<?php elseif($form->getObject()->getHeaderId()): ?>
    <?php echo jq_javascript_tag(
        'initElementBannerFileUpload("'.
            url_for('element_tempfileupload',$form->getObject()->getType()).
            '?header_id='.$form->getObject()->getHeaderId().'&portal_id='.
            $form->getObject()->getHeader()->getPortalId().
        '");'.
        'bannerBindChangeEvent();'
    )?>
<?php elseif($form->getObject()->getFooterId()): ?>
    <?php echo jq_javascript_tag(
        'initElementBannerFileUpload("'.
            url_for('element_tempfileupload',$form->getObject()->getType()).
            '?footer_id='.$form->getObject()->getFooterId().'&portal_id='.
            $form->getObject()->getFooter()->getPortalId().
        '");'.
        'bannerBindChangeEvent();'
    )?>
<?php endif; ?>
<div id="main_header" class="ui-widget-header ui-corner-all">
    <?php if($form->getObject()->exists()):?>
        <h3>Editando Banner: "<?php echo $form->getObject()->getTitle()?>"</h3>
    <?php else:?>
        <h3>Nuevo Banner</h3>
    <?php endif;?>
</div>
<div id="main_content" class="ui-widget ui-widget-content ui-corner-all">
    <div class="form">
        <?php echo jq_form_remote_tag(
                array(
                    'update' => 'main',
                    'url' => ($form->getObject()->isNew() ?
                                url_for('element_create').'?type_id='.$type->getId().'&section_id='.$section->getId()
                                :
                                url_for('element_update',$form->getObject())
                                ),
                    'before'   => 'hideMainContent();showLoaderMain();',
                    'complete' => 'hideLoader();showMainContent();',
                    'script'   => true
                ),array(
                    'id' => 'file_upload'
                ))?>
        <?php if (!$form->getObject()->isNew()): ?>
            <input type="hidden" name="sf_method" value="put" />
        <?php endif; ?>
            <fieldset>
                <?php echo $form->renderHiddenFields() ?>
                <?php echo $form->renderGlobalErrors() ?>
                <div class="form-row">
                    <?php echo $form['title']->renderError() ?>
                    <?php echo $form['title']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['title']->render() ?>
                    </div>
                </div>
                <div class="form-row">
                    <?php echo $form['Banner']['is_local']->renderError() ?>
                    <?php echo $form['Banner']['is_local']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['Banner']['is_local']->render() ?>
                    </div>
                </div>
                <div id="banner_link_local" class="form-row"
                     style="display:<?php echo ($form->getObject()->getBanner()->getIsLocal() ? 'block' : 'none')?>">
                    <?php echo $form['Banner']['section_id']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['Banner']['section_id']->render() ?>
                    </div>
                </div>
                <div id="banner_link_external" class="form-row"
                     style="display:<?php echo ($form->getObject()->getBanner()->getIsLocal() ? 'none' : 'block')?>">
                    <?php echo $form['Banner']['link']->renderError() ?>
                    <?php echo $form['Banner']['link']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['Banner']['link']->render() ?>
                        <?php echo $form['Banner']['link']->renderHelp() ?>
                    </div>
                </div>
                <?php echo  $form['Banner']['Images']->renderError() ?>
                <?php foreach ($form['Banner']['Images'] as $i => $image): ?>
                    <fieldset class="<?php echo ($i == 0 ? 'first' : '') ?>">
                        <legend><?php echo sprintf('Imagen %d', $i+1) ?></legend>
                        <div class="form-row">
                            <?php echo $image['file']->renderError() ?>
                            <?php echo $image['file_type']->renderError() ?>
                            <?php echo $image['file_size']->renderError() ?>
                            <?php echo $image['tmp_file']->renderError() ?>
                            <table id="<?php echo 'files_'.$i?>" class="upload-files-table">
                                <?php if ($image['file']->getValue()): ?>
                                    <tr>
                                        <td>Archivo actual:</td>
                                        <td class="file-preview">
                                            <img alt="<?php echo $image['file']->getValue()?>"
                                                 src="<?php echo '/uploads/portal_'.
                                                    ($form->getObject()->getSectionId() ?
                                                        $form->getObject()->getSection()->getPortalId()
                                                        :
                                                        ($form->getObject()->getHeaderId() ?
                                                            $form->getObject()->getHeader()->getPortalId()
                                                            :
                                                            $form->getObject()->getFooter()->getPortalId()
                                                        )
                                                    ).
                                                    ($form->getEmbeddedForm('Banner')->getEmbeddedForm('Images')
                                                        ->getEmbeddedForm($i)->getObject()->getFile() ?
                                                            (!$form->isBound() ?
                                                                '/images/'
                                                                :
                                                                ($image['file']->getValue() == $form->getEmbeddedForm('Banner')
                                                                                            ->getEmbeddedForm('Images')->getEmbeddedForm($i)
                                                                                            ->getObject()->getFile()
                                                                    ?
                                                                    '/images/'
                                                                    :
                                                                    '/temp_files/'
                                                                )
                                                            )
                                                            :
                                                        '/temp_files/'
                                                    ).
                                                    basename($image['file']->getValue())
                                                ?>">
                                        </td>
                                        <td class="file-size">
                                            Tamaño del archivo: <?php echo round(($image['file_size']->getValue()/1024),2) ?> Kb
                                        </td>
                                    </tr>
                                <?php endif;?>
                            </table>
                            <?php echo $image['tmp_file']->renderLabel() ?>
                            <div id="<?php echo 'drop_zone_'.$i?>" class="form-field drop-zone">
                                <?php echo $image['tmp_file']->render() ?>
                                <?php echo $image['tmp_file']->renderHelp() ?>
                                <div>Archivo a cargar</div>
                            </div>
                        </div>
                    </fieldset>
                <?php endforeach; ?>
                <div class="form-row">
                    <?php echo $form['position']->renderError() ?>
                    <?php echo $form['position']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['position']->render() ?>
                    </div>
                </div>
                <?php if(isset ($form['published'])):?>
                    <div class="form-row">
                        <?php echo $form['published']->renderError() ?>
                        <?php echo $form['published']->renderLabel() ?>
                        <div class="form-field">
                            <?php echo $form['published']->render() ?>
                        </div>
                    </div>
                <?php endif;?>
                <?php if(isset ($form['is_enabled'])):?>
                    <div class="form-row">
                        <?php echo $form['is_enabled']->renderError() ?>
                        <?php echo $form['is_enabled']->renderLabel() ?>
                        <div class="form-field">
                            <?php echo $form['is_enabled']->render() ?>
                        </div>
                    </div>
                <?php endif;?>
            </fieldset>
            <div id="form_buttons" class="ui-widget">
                <input class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-button-text"
                       type="submit" value="Guardar" title="Guardar"/>
                <?php if($form->getObject()->isNew()):?>
                    <?php echo jq_link_to_remote(
                        '<span class="ui-button-text">Cancelar</span>',
                        array(
                            'update' => 'main',
                            'url' => url_for('section_show', $form->getObject()->getSection()),
                            'before' => 'clearView();showLoaderMain();',
                            'complete' => 'hideLoader();',
                            'script' => true
                        ), array(
                            'class' => 'ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only',
                            'title' => 'Cancelar'
                        ))?>
                <?php else: ?>
                    <?php echo jq_link_to_remote(
                            '<span class="ui-button-text">Cancelar</span>',
                        array(
                            'update' => 'main',
                            'url' => url_for('element_show',$form->getObject()),
                            'before' => 'clearView();showLoaderMain();',
                            'complete' => 'hideLoader();',
                            'script' => true
                        ), array(
                            'class' => 'ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only',
                            'title' => 'Cancelar'
                        ))?>
                <?php endif; ?>
            </div>
        </form>
    </div>
</div>